import type { PageParam, PageResult } from '#/api/base';

import { requestClient } from '#/api/request';

export namespace UserApi {
  /** 用户信息 */
  export interface User {
    id?: number;
    userName: string;
    userIcon: string;
    realName?: string;
    password: string;
    mobile?: string;
    email?: string;
    companyId: number;
    departmentId: number;
    status: number;
    lastLoginTime?: string;
    isDeleted?: number;
    createTime?: string;
    updateTime?: string;
    handler?: number;
    handlerName?: string;
    companyName?: string;
    departmentName?: string;
  }

  /** 用户列表查询参数 */
  export interface UserListParams extends PageParam {
    userName?: string;
    realName?: string;
    companyId?: number;
    departmentId?: number;
    status?: number;
  }

  /** 用户角色列表项 */
  export interface UserRole {
    id: number;
    systemId: number;
    systemName: string;
    title: string;
    status: number;
    remark: string;
    createTime: string;
    updateTime: string;
    isBound: number; // 当前用户是否已绑定该角色：0-未绑定，1-已绑定
  }
}

/** 查询用户列表 */
export function getUserList(params: UserApi.UserListParams) {
  return requestClient.get<PageResult<UserApi.User>>('/user/list', {
    params,
  });
}

/** 查询用户详情 */
export function getUser(id: number) {
  return requestClient.get<UserApi.User>(`/user/info?id=${id}`);
}

/** 新增用户 */
export function createUser(data: UserApi.User) {
  return requestClient.post('/user/create', data);
}

/** 修改用户 */
export function updateUser(data: UserApi.User) {
  return requestClient.put('/user/update', data);
}

/** 删除用户 */
export function deleteUser(id: number) {
  return requestClient.delete(`/user/delete?id=${id}`);
}

/** 用户绑定角色 */
export function bindUserRole(data: { userId: number; roleId: number; bindStatus: number }) {
  return requestClient.post('/user/bind-role', data);
}

/** 查询用户角色列表 */
export function getUserRoleList(params: PageParam & { systemId?: number }) {
  return requestClient.get<PageResult<UserApi.UserRole>>('/user/role/list', {
    params,
  });
}
